using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;
using System.Data.Odbc;
using System.Windows.Forms;

using DTO;

namespace DAO
{
    public class QuaTrinhMuonDAO
    {
        DataProvider _provider = new DataProvider();
        public QuaTrinhMuonDAO()
        {
            _provider.connect();
        }
        private QuaTrinhMuonDTO GetDataFromDataRow(DataTable dt, int i)
        {
            QuaTrinhMuonDTO info = new QuaTrinhMuonDTO();
            info.ISBN = dt.Rows[i]["ISBN"].ToString();
            info.MaCuonSach = dt.Rows[i]["MaCuonSach"].ToString();
            info.MaDocGia = dt.Rows[i]["MaDocGia"].ToString();
            info.NgayMuon = DateTime.Parse(dt.Rows[i]["NgayMuon"].ToString());
            info.NgayHetHan = DateTime.Parse(dt.Rows[i]["NgayHetHan"].ToString());
            info.NgayTra = DateTime.Parse(dt.Rows[i]["NgayTra"].ToString());
            info.TienMuon = int.Parse(dt.Rows[i]["TienMuon"].ToString());
            info.TienPhat = int.Parse(dt.Rows[i]["TienPhat"].ToString());
            info.TongTien = int.Parse(dt.Rows[i]["TongTien"].ToString());
            info.GhiChu = dt.Rows[i]["GhiChu"].ToString();            
            return info;
        }

        public QuaTrinhMuonDTO GetQuaTrinhMuon(string madocgia,String isbn,String macuonsach)
        {
            string query = "select *from DocGia where MaDocGia='" + madocgia + "' and ISBN='"+isbn+"' and MaCuonSach='"+macuonsach+"'";
            OdbcDataAdapter adapter = _provider.OdbcAdapter(query);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            DataTable dt = ds.Tables[0];
            QuaTrinhMuonDTO info = new QuaTrinhMuonDTO();
            info = GetDataFromDataRow(dt, 0);
            return info;
        }
        public bool Insert(String isbn, String masach)
        {
            bool flag = true;
            if (isbn == "" || masach == "")
            {
                MessageBox.Show("Can nhap du thong tin isbn va ma sach");
                return flag = false;
            }

            //Kiem tra dau sach co ton tai trong bang dausach
            string query = "select * from DauSach where ISBN='" + isbn + "'";            
            OdbcDataAdapter adapter = _provider.OdbcAdapter(query);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("ISBN khong ton tai");
                return flag = false;
            }            
            
            //lay thong tin tu bang muon sach
            query = "select * from MuonSach where ISBN='" + isbn + "' and MaSach='" + masach+"'";
            adapter = _provider.OdbcAdapter(query);
            ds.Clear();
            adapter.Fill(ds);
            dt.Clear();
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Khong co du lieu trong bang muon sach");
                return flag = false;
            }
            MuonSachDTO info1 = new MuonSachDTO();           
            for (int i = 0; i < dt.Rows.Count; i++)
            {                
                info1.ISBN = dt.Rows[i]["ISBN"].ToString();
                info1.MaSach = dt.Rows[i]["MaSach"].ToString();
                info1.MaDocGia = dt.Rows[i]["MaDocGia"].ToString();
                info1.NgayMuon = DateTime.Parse(dt.Rows[i]["NgayMuon"].ToString());
                info1.NgayHetHan = DateTime.Parse(dt.Rows[i]["NgayHetHan"].ToString());                
                info1.SoLuongMuon = int.Parse(dt.Rows[i]["SoLuongMuon"].ToString());
                info1.SoLuongTra = int.Parse(dt.Rows[i]["SoLuongTra"].ToString());                
            }
            DateTime temp = DateTime.Now;         
            //Lay tien muon tu bang dausach
            query = "select * from DauSach where ISBN='" + isbn + "'";
            adapter = _provider.OdbcAdapter(query);
            ds.Clear();
            adapter.Fill(ds);
            dt.Clear();
            String tienmuon = dt.Rows[0]["DonGiaDauSachMuon"].ToString();
            //lay ghi chu o bang dau sach                        
            String ghichu = dt.Rows[0]["GhiChu"].ToString();            
            //tinh tien phat
            int tienphat = 0;
            if (temp.CompareTo(info1.NgayHetHan) > 0)
            {
                tienphat = int.Parse(tienmuon) * 10 / 100;
            }
            //tinh tong tien
            int tongtien = int.Parse(tienmuon) + tienphat;
            //insert vao bang qua trinh muon
           string cmdInsert = "Insert into QuaTrinhMuon values('"+info1.ISBN +
           "','"+info1.MaSach+"','" + info1.MaDocGia +"','"+info1.NgayMuon+
            "','" + info1.NgayHetHan + "','" + temp + "','" + tienmuon + 
            "','" + tienphat + "','" + tongtien+ "','" + ghichu + "')";
            _provider.executeNonQuery(cmdInsert);
            return flag;
        }
    }
}
